/**
 * Moondance UML modeling tool
 * 
 * @author Dascalu Laurentiu (dascalu.laurentziu@gmail.com).
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.internal.wizards.datatransfer.DataTransferMessages;
import org.eclipse.ui.internal.wizards.datatransfer.MinimizedFileSystemElement;
import org.eclipse.ui.internal.wizards.datatransfer.WizardFileSystemResourceImportPage1;

@SuppressWarnings("restriction")
public class XMIPageSerializedReader extends
		WizardFileSystemResourceImportPage1 {
	public final static String XMI_EXTENSION = ".xmi";

	// Selected file names
	private Vector<String> fileNames;

	// Project used as destination for the import operation
	private String projectImportDestionation;

	public XMIPageSerializedReader(IWorkbench aWorkbench,
			IStructuredSelection selection) {
		super(aWorkbench, selection);
		fileNames = new Vector<String>();
	}

	@SuppressWarnings("unchecked")
	@Override
	protected boolean validateSourceGroup() {
		File sourceDirectory = getSourceDirectory();
		if (sourceDirectory == null) {
			setMessage(SOURCE_EMPTY_MESSAGE);
			enableButtonGroup(false);
			return false;
		}

		if (sourceConflictsWithDestination(new Path(sourceDirectory.getPath()))) {
			setMessage(null);
			setErrorMessage(getSourceConflictMessage());
			enableButtonGroup(false);
			return false;
		}

		List resourcesToExport = selectionGroup.getAllWhiteCheckedItems();
		if (resourcesToExport.size() == 0) {
			setMessage(null);
			setErrorMessage(DataTransferMessages.FileImport_noneSelected);
			return false;
		}

		boolean valid = true;
		fileNames.clear();

		/**
		 * Check if all selected files end with .xmi; also save the name of
		 * selected files
		 */
		for (Iterator it = resourcesToExport.iterator(); it.hasNext();) {
			MinimizedFileSystemElement currentMFSE = (MinimizedFileSystemElement) it
					.next();
			File currentFile = (File) currentMFSE.getFileSystemObject();
			fileNames.add(currentFile.getName());
			if (!currentFile.getName().endsWith(XMI_EXTENSION))
				valid = false;
		}

		if (!valid) {
			setMessage(null);
			setErrorMessage("Import only XMI files");
			return false;
		}

		setProjectImportDestionation(getContainerFullPath().toOSString());

		enableButtonGroup(true);
		setErrorMessage(null);
		return true;
	}

	public Vector<String> getSelectedFileNames() {
		return fileNames;
	}

	public void setProjectImportDestionation(String projectImportDestionation) {
		this.projectImportDestionation = projectImportDestionation;
	}

	public String getProjectImportDestionation() {
		return projectImportDestionation;
	}
}
